﻿Imports System.Reflection
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Public Class GlamUp
    Inherits DataContext
    Public Brands As Table(Of Brand)
    Public Images As Table(Of Image)
    Public Categories As Table(Of Category)
    Public Products As Table(Of Product)

    <FunctionAttribute(Name:="dbo.GetAvailableBrands")> _
    Public Function GetAvailableBrands(<Parameter(DbType:="VarChar(50)")> ByVal categoryId As String) As ISingleResult(Of GetAvailableBrandsResult)
        Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), categoryId)
        Return CType(result.ReturnValue, ISingleResult(Of GetAvailableBrandsResult))
    End Function

    Public Sub New(ByVal conn As String)
        MyBase.New(conn)
    End Sub

End Class





<Table(Name:="brands")> _
Public Class Brand
    <Column(Name:="brand_id", Storage:="_brandId", IsPrimaryKey:=True)> _
    Private _brandId As String
    Public Property BrandId() As String
        Get
            Return Me._brandId
        End Get
        Set(ByVal value As String)
            Me._brandId = value
        End Set
    End Property

    <Column(Name:="brand_name", Storage:="_brandName")> _
    Private _brandName As String
    Public Property BrandName() As String
        Get
            Return Me._brandName
        End Get
        Set(ByVal value As String)
            Me._brandName = value
        End Set
    End Property


End Class

<Table(Name:="categories")> _
Public Class Category
    <Column(Name:="category_id", IsPrimaryKey:=True)> _
    Public categoryId As String

    <Column(Name:="category_name")> _
    Public categoryName As String

End Class

<Table(Name:="products")> _
Public Class Product
    <Column(Name:="product_id", IsPrimaryKey:=True)> _
    Public productId As String

    '<Column(Name:="category_id")> _
    'Public categoryId As Int32

    '<Column(Name:="brand_id")> _
    'Public brandId As Int32


    <Column(Name:="product_name", Storage:="_productName")> _
    Private _productName As String
    Public Property ProductName() As String
        Get
            Return Me._productName
        End Get
        Set(ByVal value As String)
            Me._productName = value
        End Set
    End Property

    <Column(Name:="sku")> _
    Public sku As String


    <Column(Name:="brand_id")> _
    Public brandId As String

    <Column(Name:="category_id")> _
    Public categoryId As String

    Private _brand As EntityRef(Of Brand)
    <Association(Name:="FK_products_categories1", Storage:="_brand", ThisKey:="brandId", IsForeignKey:=True)> _
    Public Property Brand() As Brand
        Get
            Return (Me._brand.Entity)
        End Get
        Set(ByVal value As Brand)
            Dim previousValue As Brand = Me._brand.Entity
            If ((Object.Equals(previousValue, value) = False) _
               OrElse (Me._brand.HasLoadedOrAssignedValue = False)) Then
                Me._brand.Entity = Nothing

            End If
            Me._brand.Entity = value
            Me.brandId = value.brandId

        End Set
    End Property

    Private _category As EntityRef(Of Category)
    <Association(Name:="FK_products_categories", Storage:="_category", ThisKey:="categoryId", IsForeignKey:=True)> _
    Public Property Category() As Category
        Get
            Return (Me._category.Entity)
        End Get
        Set(ByVal value As Category)
            Dim previousValue As Category = Me._category.Entity
            If ((Object.Equals(previousValue, value) = False) _
               OrElse (Me._category.HasLoadedOrAssignedValue = False)) Then
                Me._category.Entity = Nothing
            End If
            Me._category.Entity = value
            Me.categoryId = value.categoryId

        End Set
    End Property

    Private _images As EntitySet(Of Image)
    <Association(Name:="FK_images_products", Storage:="_images", OtherKey:="productId")> _
    Public Property Images() As EntitySet(Of Image)
        Get
            Return Me._images
        End Get
        Set(ByVal value As EntitySet(Of Image))
            Me._images.Assign(value)
        End Set
    End Property
    Public ReadOnly Property GetMainImage() As Image
        Get
            'Dim ImageTable As IQueryable(Of Image) = Me._images.AsQueryable
            'Dim image = From img In Me._images.AsQueryable Where img.isMain Select img
            'Return image
            For Each Image As Image In Me._images
                If Image.isMain Then
                    Return Image
                End If
            Next
            Return Nothing
        End Get
    End Property


    Public Sub New()
        Me._images = New EntitySet(Of Image)
    End Sub

End Class

<Table(Name:="images")> _
Public Class Image
    <Column(Name:="image_id", IsPrimaryKey:=True)> _
    Public imageId As String

    <Column(Name:="product_id")> _
    Public productId As String

    <Column(Name:="path", Storage:="_path")> _
    Private _path As String
    Public Property Path() As String
        Get
            Return Me._path
        End Get
        Set(ByVal value As String)
            Me._path = value
        End Set
    End Property

    <Column(Name:="is_main")> _
    Public isMain As Boolean

End Class


Public Class GetAvailableBrandsResult

    Private _brand_id As String

    Private _product_count As System.Nullable(Of Integer)

    Private _brand_name As String

    Public Sub New()
        MyBase.New()
    End Sub

    <Column(Storage:="_brand_id", DbType:="VarChar(50)", Name:="brand_id")> _
    Public Property BrandId() As String
        Get
            Return Me._brand_id
        End Get
        Set(ByVal value As String)
            If (String.Equals(Me._brand_id, value) = False) Then
                Me._brand_id = value
            End If
        End Set
    End Property

    <Column(Storage:="_product_count", DbType:="Int", Name:="product_count")> _
    Public Property Count() As System.Nullable(Of Integer)
        Get
            Return Me._product_count
        End Get
        Set(ByVal value As System.Nullable(Of Integer))
            If (Me._product_count.Equals(value) = False) Then
                Me._product_count = value
            End If
        End Set
    End Property

    <Column(Storage:="_brand_name", DbType:="NVarChar(100)", Name:="brand_name")> _
    Public Property BrandName() As String
        Get
            Return Me._brand_name
        End Get
        Set(ByVal value As String)
            If (String.Equals(Me._brand_name, value) = False) Then
                Me._brand_name = value
            End If
        End Set
    End Property
End Class
